package com.yc.cloud.portal.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.api.service.SysDictCacheService;
import com.yc.cloud.basic.constants.RedisKeyConstants;
import com.yc.cloud.basic.dto.BaseQueryParam;
import com.yc.cloud.entity.SysDict;
import com.yc.cloud.mapper.SysDictMapper;
import com.yc.cloud.portal.service.SysDictService;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2024-01-24
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {


    private final SysDictCacheService sysDictCacheService;

    public SysDictServiceImpl(SysDictCacheService sysDictCacheService) {
        this.sysDictCacheService = sysDictCacheService;
    }

    @Override
    public IPage<SysDict> list(BaseQueryParam queryParam) {
        val query = new QueryWrapper<SysDict>();
        IPage<SysDict> page;
        //排序
        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
            if (BaseQueryParam.ORDER_DESC.equals(queryParam.getOrder())) {
                query.orderByDesc(queryParam.getSortBy());
            } else if (BaseQueryParam.ORDER_ASC.equals(queryParam.getOrder())) {
                query.orderByAsc(queryParam.getSortBy());
            }
        }
        //模糊查询
        if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
            query.and(queryWrapper -> {
                queryWrapper.like(SysDict.DICT_TEXT, queryParam.getKeyword()).or().like(SysDict.DICT_VALUE, queryParam.getKeyword());
            });
        }
        //分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), baseMapper.selectCount(query), true);
        }
        return baseMapper.selectPage(page, query);
    }

    @Override
    public SysDict create(SysDict param) {
        val sysdict = new SysDict();
        BeanUtils.copyProperties(param, sysdict);
        val saveFlag = save(sysdict);
        return saveFlag ? sysdict : null;
    }

    @Override
    public SysDict get(Long id) {
        return getById(id);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public SysDict getByDictName(String dictText) {
        val key = RedisKeyConstants.SYS_DICTS + ":" + dictText;
        if (sysDictCacheService.existByKey(key)) {
            return sysDictCacheService.getByKey(key);
        }
        val sysDict = getByDictNameFromDb(dictText);
        if (sysDict != null) {
            sysDictCacheService.setByKey(key, sysDict);
        }
        return sysDict;
    }

    private SysDict getByDictNameFromDb(String dictText) {
        return baseMapper.selectOne(new QueryWrapper<SysDict>().eq(SysDict.DICT_TEXT, dictText));
    }

    @Override
    public SysDict update(Long id, SysDict param) {
        val sysdict = new SysDict();
        BeanUtils.copyProperties(param, sysdict);
        sysdict.setId(id);
        val updateFlag = updateById(sysdict);
        return updateFlag ? sysdict : null;
    }


    @Override
    public SysDict convertVo(SysDict sysdict) {
        if (sysdict == null) {
            return null;
        }
        val vo = new SysDict();
        BeanUtils.copyProperties(sysdict, vo);
        return vo;
    }

    @Override
    public List<SysDict> convertVo(List<SysDict> SysDictList) {
        if (CollUtil.isNotEmpty(SysDictList)) {
            return SysDictList.stream().map(this::convertVo).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
